package com.wh1200.mybatis.xmlless.plugin.visitor

import com.wh1200.mybatis.xmlless.util.getXmllessTableInfo
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter
import net.sf.jsqlparser.schema.Column

class ReplaceExpressionVisitor(private val tables: Map<String, Class<*>>) : ExpressionVisitorAdapter() {

  private val properties = mutableListOf<String>()

  override fun visit(col: Column) {
    val model = if (col.table != null) {
      tables[col.table.name]
    } else if (tables[""] != null) {
      tables[""]
    } else {
      null
    }
    if (model == null) {
      return
    }
    val tableInfo = getXmllessTableInfo(model)
    val field = tableInfo.tableFieldMap[col.columnName]
    if (field != null) {
      properties.add(col.columnName)
      col.columnName = field.column
    }
  }

  fun getProperties(): List<String> {
    return properties
  }

}
